<script setup lang="ts">
import { ref } from "vue";
import { useAdAccount } from "./utils/hook";
import { transformI18n } from "@/plugins/i18n";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";

const { t } = useI18n();

// 图标导入
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";
import Download from "@iconify-icons/ep/download";

defineOptions({
  name: "AdAccount"
});

const formRef = ref();
const tableRef = ref();

const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  agentOptions,
  selectedRows,
  onSearch,
  resetForm,
  openDialog,
  handleDelete,
  handleExport,
  handleSelectionChange,
  handleSizeChange,
  handleCurrentChange
} = useAdAccount();

function onFullscreen() {
  // 重置表格高度
  tableRef.value.setAdaptive();
}

// 批量删除
function handleBatchDelete() {
  if (selectedRows.value.length === 0) {
    ElMessage.warning("请选择要删除的数据");
    return;
  }
  handleDelete();
}
</script>

<template>
  <div class="main">
    <!-- 搜索表单 -->
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="账号" prop="account_no">
        <el-input
          v-model="form.account_no"
          placeholder="请输入账号"
          clearable
          class="!w-[200px]"
        />
      </el-form-item>
      
      <el-form-item label="代理商" prop="ad_agent_id">
        <el-select
          v-model="form.ad_agent_id"
          placeholder="请选择代理商"
          clearable
          class="!w-[200px]"
          filterable
        >
          <el-option
            v-for="agent in agentOptions"
            :key="agent.value"
            :label="agent.label"
            :value="agent.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="关键词" prop="keyword">
        <el-input
          v-model="form.keyword"
          placeholder="搜索账号或备注"
          clearable
          class="!w-[200px]"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="onSearch"
        >
          {{ t("buttons.search") }}
        </el-button>
        <el-button
          :icon="useRenderIcon(Refresh)"
          @click="resetForm(formRef)"
        >
          {{ t("buttons.reset") }}
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 表格工具栏 -->
    <PureTableBar
      title="广告账号管理"
      :columns="columns"
      :isExpandAll="false"
      :tableRef="tableRef?.getTableRef()"
      @refresh="onSearch"
      @fullscreen="onFullscreen"
    >
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="openDialog()"
        >
          {{ t("buttons.add") }}
        </el-button>
        
        <el-button
          type="danger"
          :icon="useRenderIcon(Delete)"
          :disabled="selectedRows.length === 0"
          @click="handleBatchDelete"
        >
          批量删除
        </el-button>

        <!-- 导出按钮暂时隐藏
        <el-button
          type="success"
          :icon="useRenderIcon(Download)"
          @click="handleExport"
        >
          导出数据
        </el-button>
        -->
      </template>

      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          adaptive
          :adaptiveConfig="{ offsetBottom: 45 }"
          align-whole="center"
          row-key="id"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :size="size"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="pagination"
          :paginationSmall="size === 'small'"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="openDialog('修改', row)"
            >
              {{ t("buttons.edit") }}
            </el-button>

            <el-popconfirm
              :title="`确认删除账号 ${row.account_no} 吗？`"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  {{ t("buttons.delete") }}
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-table__inner-wrapper::before) {
  height: 0;
}

.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}

:deep(.el-table) {
  .el-table__cell {
    padding: 8px 0;
  }
}

// 表格内标签样式
:deep(.el-tag) {
  margin: 2px;
}

// 操作按钮样式
:deep(.el-button--small) {
  margin-left: 4px;
  padding: 4px 8px;
}

// 搜索表单响应式
@media (max-width: 768px) {
  .search-form {
    :deep(.el-form-item) {
      margin-bottom: 8px;
      margin-right: 8px;
    }
    
    :deep(.el-input) {
      width: 160px !important;
    }
    
    :deep(.el-select) {
      width: 160px !important;
    }
  }
}
</style>
